package jniGlue;

import java.io.File;

import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;

import programm.OwnFileChooser;

/**
 * Class for linking to the liblinuxdmx.so which specifies the connection to the
 * FX5-DMX Interface. Only for Linux (i am using Ubuntu 11.10 right now)!
 * 
 * @author jm
 * 
 */
public class Linuxclass implements Librarylinker
{
	private String sPathArchive;
	private String sPath;
	private String usbdmxpath;

	public native void startdmx(String path);

	public native void senddmx(int channel, int value);

	public native void closedmx(String path);

	public boolean success = false;

	public Linuxclass(String sPathArchive)
	{
		{
			this.sPathArchive = sPathArchive;
			sPath = sPathArchive + "/src/LinuxSource/";

			try
			{
				System.load(sPath + "liblinuxdmx.so");
				startdmx("");
				success = true;
				// Everything went well!
			} catch (UnsatisfiedLinkError ue)
			{
				JOptionPane
						.showMessageDialog(
								new JDialog(),
								"Die Bibliothek liblinuxdmx.so konnte nicht gefunden werden! Bitte geben Sie sie nun an.",
								"Bibliothek nicht gefunden!",
								JOptionPane.ERROR_MESSAGE);

				// Create a file chooser
				sPath = (new OwnFileChooser(".so",
						"Bitte liblinuxdmx.so auswaehlen!").getPath());

				if (sPath == null)
				{
					// Then the CANCEL button was pressed
					success = false;
					JOptionPane.showMessageDialog(new JDialog(),
							"Abbruch! Programm wird beendet!", "Abbruch!",
							JOptionPane.ERROR_MESSAGE);
				} else
				{
					if (sPath.indexOf("liblinuxdmx.so") != -1)
					{
						// load the Wrapperlibrary
						System.load(sPath);

						// start the Interface
						this.startdmx(sPath);
						success = true;
						// Everything went well!

					} else
					{
						// Then a wrong lib was selected
						success = false;
						JOptionPane
								.showMessageDialog(
										new JDialog(),
										"Falsche Bibliothek ausgewaehlt! Programm wird beendet!",
										"Fehler!", JOptionPane.ERROR_MESSAGE);

					}
				}
			}
		}
	}

	/**
	 * This Path is needed in the closedmx() function when the OS is Windows.
	 * When OS is Linux then an empty String is returned.
	 * 
	 * @return
	 */
	public String getUsbdmxPath()
	{
		return "";
	}

}

/*
 * ANLEITUNG: LIBUSBDMX.SO cd /home/jm/workspace/usb2dmx/src/LinuxSource/ gcc
 * hid.o usbdmx.o -shared -o libusbdmx.so LIBLINUXDMX.SO cd .. javac
 * jniGlue/Linuxclass.java javah jniGlue.Linuxclass mv ./jniGlue_Linuxclass.h
 * ./LinuxSource/ cd LinuxSource/ gcc hid.o usbdmx.o -shared
 * -I/usr/lib/jvm/java-6-openjdk/include java_interface.cpp -o liblinuxdmx.so
 * -ludev
 */